# This code computes local overexposure by ori9 and adds other agency level characteristics
# as required to create Figures 5, S9, S10, S11

library(tidyverse)
load("combined_dta_long_483.rdata")

exp_weight = function(x, decay=0.00005, threshold = 483){
  y = exp(-decay*(x^2))
  y[x>threshold] = 0
  return(y)
}

dta = dta %>%
  mutate(
    w = exp_weight(d)
  ) %>%
  filter(year > 2009 & year < 2020)

ori9_post = dta %>%
  filter(type=='post') %>%
  group_by(agency) %>%
  summarize(
    black_post = sum((black>0)*w, na.rm=T) / sum(w),
    sum_w_posts = sum(w),
    n_posts = n(),
    n_posts_self = sum(w==1)
  )

ori9_arrest = dta %>%
  filter(type=='arrest') %>%
  group_by(agency) %>%
  summarize(
    black_arrest = sum((black>0)*w, na.rm=T) / sum(w),
    n_arrests = n()
  )

dta_ori9 = full_join(ori9_post, ori9_arrest) %>%
  mutate(
    overexposure = black_post - black_arrest
  )

dta_agency = read_csv("agency_data.csv")
dta_ori9 = dta_ori9 %>%
  left_join(dta_agency)

dta_ori9 = dta_ori9 %>%
  mutate(
  sheriffs_office = grepl("sheriff", name)
)
dta_ori9 %>%
  write_csv("overexposure_by_ori9.csv")
